import java.util.Arrays;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-08-06
 * Time: 9:55
 */
public class SortTest {
    public void shellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            gap = gap / 2;
            shell(array, gap);
        }
    }
    private void shell(int[] array, int gap){
        for(int i = gap; i < array.length; i++){
            int tmp = array[i];
            int end = i - gap;
            while(end >= 0){
                if(array[end] > tmp){
                    array[end + gap] = array[end];
                    end -= gap;
                }else{
                    break;
                }
            }
            array[end + gap] = tmp;
        }
    }
    public void heapSort(int[] array){
        //建堆
        for(int i = (array.length - 1 - 1) / 2; i >= 0; i--){
            siftDown(array, i, array.length);
        }
        int end = array.length - 1;
        while(end > 0){
            swap(array, 0, end);
            siftDown(array, 0, end);
            end--;
        }
    }
    private void siftDown(int[] array, int parent, int n){
        int child = 2 * parent + 1;
        while(child < n){
            if(child + 1 < n && array[child + 1] > array[child]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;
            }else{
                break;
            }
        }
    }
    private void swap(int[] array, int x, int y){
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }
    public void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }
    private void quick(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        //获取基准值
        if(right - left + 1 <= 10){
            insertRange(array, left, right);
        }
        int index = getThreeMid(array, left, right);    //获取中间值下标, 处理有序的情况单支树
        swap(array, left, index); //把第一个元素换成三数取中后的基准值
        int par = parttionHoare(array,left, right);
        quick(array, left, par - 1);
        quick(array, par + 1, right);

    }
    private int parttionHoare(int[] array, int low, int high) {//hoare版本
        int key = array[low];
        int par = low;
        while(low < high){
            while(low < high && array[high] >= key){
                high--;
            }
            while(low < high && array[low] <= key){
                low++;
            }
            swap(array, low, high);
        }
        swap(array, par, low);
        return low;
    }
    private int parttionHole(int[] array, int low, int hign){
        int key = array[low];
        while(low < hign){
            while(low < hign && array[hign] >= key){
                hign--;
            }
            array[low] = array[hign];
            while(low < hign && array[low] <= key){
                low++;
            }
            array[hign] = array[low];
        }
        array[low] = key;
        return low;
    }
    private int getThreeMid(int[] array, int left, int right){
        int mid = (left + right) / 2;
        if(array[left] < array[right]){
            if(array[mid] < array[right]){
                return mid;
            }else if(array[right] < array[mid]){
                return right;
            }else{
                return left;
            }
        }else{
            if(array[mid] > array[left]){
                return left;
            }else if(array[mid] > array[right]){
                return mid;
            }else{
                return right;
            }
        }
    }
    private void insertRange(int[] array, int low, int hign){
        for(int i = low + 1; i <= hign; i++){
            int tmp = array[i];
            int end = i - 1;
            while(end >= low){
                if(array[end] > tmp){
                    array[end + 1] = array[end];
                    end--;
                }else{
                    break;
                }
            }
            array[end + 1] = tmp;
        }
    }
    public void quickSortNor(int[] array){
       int low = 0;
       int hign = array.length - 1;
       int par = parttionHole(array, low, hign);
       Stack<Integer> st = new Stack<>();
       if(par > low + 1){
           st.push(low);
           st.push(par - 1);
       }
       if(par < hign - 1){
           st.push(par + 1);
           st.push(hign);
       }
       while(!st.empty()){
           hign = st.pop();
           low = st.pop();
           par = parttionHole(array, low, hign);
           if(par > low + 1){
               st.push(low);
               st.push(par - 1);
           }
           if(par < hign - 1){
               st.push(par + 1);
               st.push(hign);
           }
       }
    }
    public void mergeSort(int[] array){
        mergeSortChild(array, 0, array.length - 1);
    }
    private void mergeSortChild(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        int mid = (left + right) / 2;
        mergeSortChild(array, left, mid );
        mergeSortChild(array, mid + 1, right);
        merge(array, left, mid, right);
    }
    private void merge(int[] array, int left, int mid, int right){
        int[] tmp = new int[right - left + 1];
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int k = 0;
        while(s1 <= e1 && s2 <= e2){
            if(array[s1] < array[s2]){
                tmp[k++] = array[s1++];
            }else{
                tmp [k++] = array[s2++];
            }
        }
        while(s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while(s2 <= e2){
            tmp[k++] = array[s2++];
        }
        for(int i = 0; i < tmp.length; i++){
            array[i + left] = tmp[i];
        }
    }
}
